package win.smartown.android.base.loading

import android.os.Bundle
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
import android.view.View
import android.view.ViewStub
import win.smartown.android.R
import win.smartown.android.base.BaseActivity
import win.smartown.android.base.loading.state.loading.LoadingView
import win.smartown.android.viewController.loading.DefaultEmptyView
import win.smartown.android.viewController.loading.DefaultLoadingView
import win.smartown.android.viewController.loading.EmptyView

/**
 * Created by smartown on 2017/3/18.
 * <br></br>
 * Description:带有loading提示的activity
 */
open class LoadingActivity : BaseActivity(), LoadingView, EmptyView {

    private var loadingContentView: View = null!!
    private var emptyView: EmptyView? = null
    private var loadingView: LoadingView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setBaseContentView(R.layout.activity_loading)
        loadingView = setupLoadingView(super.findView<View>(R.id.loading_loading) as ViewStub)
        emptyView = setupEmptyView(super.findView<View>(R.id.loading_unusual) as ViewStub)
    }

    protected fun setLoadingContentView(@LayoutRes id: Int) {
        val viewStub = super.findView<ViewStub>(R.id.loading_content)
        viewStub.layoutResource = id
        loadingContentView = viewStub.inflate()
    }

    override fun <T : View> findView(@IdRes id: Int): T {
        return loadingContentView.findViewById(id) as T
    }

    protected fun setupLoadingView(loadingViewStub: ViewStub): LoadingView {
        return DefaultLoadingView(loadingViewStub)
    }

    override fun startLoading() {
        loadingView!!.startLoading()
    }

    override fun finishLoading() {
        loadingView!!.finishLoading()
    }

    protected fun setupEmptyView(emptyViewStub: ViewStub): EmptyView {
        return DefaultEmptyView(emptyViewStub)
    }

    override fun resetData() {
        emptyView!!.resetData()
    }

    override fun noData() {
        emptyView!!.noData()
    }

}
